clear

set seed 1073741823

capt ssc install randomselect


* ########## WAVE 1 ##########	

	
/* First session */
insheet using "${pathdata_mechanism}/MechanismExperiment2_raw_wave1.csv", names
drop in 1/2

drop if prolific_pid == "{{%PROLIFIC_PID%}}"
drop if prolific_pid == ""



destring *, replace 

	keep if main == 1

	keep if writedowninfo==2
	
	gen random_draw = runiform()
	tab random_draw

	// ONLY NEEDED FOR BONUS PAYMENTS
	//draw selected participants for bonus
	randomselect, gen(sample) prop(0.1)
	tab sample

	// Randomly draw participants who receive a bonus (30$) for the belief stated in 1 and 2, or the structured recall question (denoted by 3 for typeofinfo_recall and 4 for valence_recall)
	gen bonus_belief = .
	replace bonus_belief = 1 if sample == 1 & random_draw <= 0.5
	replace bonus_belief = 2 if sample == 1 & random_draw > 0.5 & random_draw <= 0.75
	replace bonus_belief = 3 if sample == 1 & random_draw > 0.75 & random_draw <= 0.875
	replace bonus_belief = 4 if sample == 1 & random_draw > 0.875

	// Randomly draw product for which the bonus payment (30$) is carried out, 

	randomselect if sample == 1, gen(foodtruck_) prop(0.333)
	randomselect if sample == 1 & foodtruck_ == 0, gen(sportsstadium_) prop(0.5)

	// Note 1: videogame; 2: bicycle; 3: restaurant
	gen bonusproduct = ""
	replace bonusproduct = "amusementpark" if sample == 1 & foodtruck_ ==0 & sportsstadium_==0
	replace bonusproduct = "foodtruck" if sample == 1 & foodtruck_ ==1
	replace bonusproduct = "sportsstadium" if sample == 1 & sportsstadium_ ==1
	tab bonusproduct

	/* Variables to keep */
	keep p_guess* task* prolific_pid valence* treatment truepos_fraction* truepos_reviews* sample bonus_belief bonusproduct qid9 qid124 qid12 qid18 durationinseconds finished comprehension1 comprehension2 comprehension3 comprehension5 screenedout writedowninfo

			
		rename qid9 gender 

		rename qid124 age

		rename qid12 education

		rename qid18 employment
		
		
		
		*time spent on decision page for videogame, restaurant and bicycle
		*rename _q230_pagesubmit pagetime_videogame
		
		*rename v39 pagetime_restaurant
		

		* Indicator Male
		gen male = .
		replace male = 0 if inlist(gender, 2,3)
		replace male = 1 if gender == 1

		* Indicator Bachelor's degree or more
		gen college = .
		replace college = 0 if inlist(education, 1,2,3,4)
		replace college = 1 if inlist(education, 5,6,7,8)

		* Indicator Employment status
		gen employed = .
		replace employed = 0 if inlist(employment,3,4)
		replace employed = 1 if inlist(employment,1,2)
		
		* Indicator finished wave 1 - EMPLOYMENT IS THE LAST MANDATORY QUESTION IN THE SURVEY
		gen completed_wave1 = 0
		replace completed_wave1 = 1 if inlist(employment,1,2,3,4)

		
		

		duplicates drop prolific_pid, force
		
		

		/* Wide to long */
		reshape long valence_ p_guess_ wtp_ task_ truepos_fraction_ pagetime_, i(prolific_pid) j(product) string

		
		* Compute bayesian posterior
		gen total_reviews = -9999
		replace total_reviews = 14 if product=="foodtruck"
		replace total_reviews = 19 if product=="sportsstadium"
		replace total_reviews = 17 if product=="amusementpark"
		
		gen sample_reviews = 1
		
		gen sample_positive_reviews = 0
		replace sample_positive_reviews = 1 if product == "foodtruck"
		

		gen bayesian_posterior = sample_positive_reviews/total_reviews + (total_reviews-sample_reviews) / total_reviews * (sample_positive_reviews+1)/(sample_reviews+2)
		
		gen bayesian_movement = abs(bayesian_posterior - 0.5)

		
		
		
		
		
	
	
		encode treatment, gen(treatment2)
		

		
		gen abs_belief_update = abs(bayesian_posterior - 0.5)
		
		mean abs_belief_update, over(treatment2)
		

*/
save "${pathdata_mechanism}/temp/MechanismExperiment2_long.dta", replace

	
* ########## Prolific IDs only ##########	
/*
duplicates drop prolific_pid, force
drop if missing(prolific_pid)
keep prolific_pid

save "prolific_pid_only.dta", replace
*/

* ########## WAVE 2 ##########	
	
clear

/* Second (recall) session */
insheet using "${pathdata_mechanism}/MechanismExperiment2_raw_wave2.csv", names
drop in 1/2

destring *, replace
	
	*** give w2 duration in seconds a new name so it does not overwrite wave1
	rename durationinseconds durationinseconds2
	
	/* Variables to keep */	
	keep beliefs_* valence_recall_* typeofinfo_recall_* prolific_pid v44 v50 v33 v32 


	drop if prolific_pid == ""
	sort prolific_pid
	quietly by prolific_pid:  gen dup = cond(_N==1,0,_n)
	drop if dup > 0
	drop dup

	/*
	* CAUTION: WE NEED TO RECODE THIS BECAUSE SOMETHING WENT WRONG IN QUALTRICS. SPECIFICALLY, SOME BELIEFS WHERE CODED IN V26 and V32 AND BELIEFS_FIRST. WE CORRECT THIS HERE.
	replace beliefs_videogame = _beliefs_first if beliefs_videogame ==.
	replace beliefs_bicycle = v26 if beliefs_bicycle ==.
	replace beliefs_restaurant = v32 if beliefs_restaurant ==.
	*/

	

	/* Wide to long */
	reshape long beliefs_ typeofinfo_recall_ valence_recall_, i(prolific_pid) j(product) string
	
		
	
	

	*rename _open_endedfirst text_videogame
	*replace text_videogame = _openended_second if text_videogame == ""

	*rename v44 text_bicycle
	*replace text_bicycle = v27 if text_bicycle == ""

	*rename v50 text_restaurant
	*replace text_restaurant = v33 if text_restaurant == ""

	*drop _openended_second v27 v33

	*gen free_form = ""
	*replace free_form = text_videogame if product =="videogame"
	*replace free_form = text_bicycle if product =="bicycle"
	*replace free_form = text_restaurant if product =="restaurant"
	*drop text*

save "${pathdata_mechanism}/temp/MechanismExperiment2_long_followup.dta", replace

* ########## MERGE WAVE 1 and WAVE 2 ##########	
	

	/* Merged */
	merge 1:1 prolific_pid product using "${pathdata_mechanism}/temp/MechanismExperiment2_long.dta"
	
* ########## CREATE DATA ON ATTRITION IN BETWEEN SUBJECT TREATMENTS ##########	

	
	* indicator whether subjects completed study 2
	gen completed = 0
	replace completed = 1 if _merge ==3
	
	
	* ##### Generate dataset with treatments and completion only
preserve
	* drop NAs
	drop if _merge==1


	gen diff = 50 - p_guess_
	
	gen effect = diff
	replace effect = p_guess_ - 50 if valence_== "pos"
	
	tab effect
	
	
	
	gen wrong_update = 0
	replace wrong_update = 1 if effect < 0
	
	egen any_wrong_update = max(wrong_update), by(prolific_pid)
	
	drop if any_wrong_update > 0
	
	drop if p_guess_ == .
	
	keep prolific_pid completed treatment
		
	duplicates drop
	
	save "${pathdata_summary}/CuenontargetAttrition.dta", replace
	
restore
	
		
	keep if _merge==3
	drop _merge

	destring*, replace

	/* Exclusion Criteria */
	drop if screenedout == "True"

save "${pathdata_mechanism}/temp/MechanismExperiment2_merged.dta", replace





* ########## ADD VARIABLES TO MERGED DATA ##########	


/* Variables for analysis */

	/* 1 - Effect variables */
	gen diff = 50 - p_guess_
	gen diff_recall = 50 - beliefs_


	gen effect = diff
	replace effect = p_guess_ - 50 if product == "foodtruck"

	gen effect_recall = diff_recall
	replace effect_recall = beliefs_ - 50 if product == "foodtruck"
	

	/* Drop missing products */
	drop if p_guess_ == .

	preserve


	*stop /// the stop command was not recognized on my STATA version, so I commented it out (Simon, 22/10/30)

	/* Drop observations where updating is in the wrong direction */

		tab effect

		drop if effect < 0
		
		bysort prolific_pid : drop if _N==1

		tab prolific_pid
		
		
		
* ########## STRUCTURED RECALL ##############
		
	gen correct_recall = 0
	
	replace correct_recall = 1 if product== "foodtruck" & typeofinfo_recall_ == "Information on a <strong>single</strong> review plus some additional anecdotal details about the reviewer and their experience" & valence_recall_ == "Positive" 
	
	replace correct_recall = 1 if product== "amusementpark" & typeofinfo_recall_ == "Information on a <strong>single</strong> review plus some additional anecdotal details about the reviewer and their experience" & valence_recall_ == "Negative" 
	
	replace correct_recall = 1 if product== "sportsstadium" & typeofinfo_recall_ == "Information on a <strong>single</strong> review plus some additional anecdotal details about the reviewer and their experience" & valence_recall_ == "Negative" 
	
		
		
	
		
		
		
		
tab treatment
		

save "${pathdata_mechanism}/MechanismExperiment2.dta", replace


* ##### Save data for summary statistics

	keep prolific_pid male age college employed

	duplicates drop

	save "${pathdata_summary}/CuenontargetSummary.dta", replace



restore




